package pers.crayon.baseprogram.task;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 线程池创建工具类(直接用Executors就好)
 *
 * @author wangzg13
 * @since 2022/10/16
 */
public class TaskExecutors {

    /**
     * 创建定时任务池
     */
    private static final ScheduledExecutorService scheduledThreadPoolExecutor = Executors.newScheduledThreadPool(5);

    private static final ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

    static {
        // 需要执行Init初始化方法
        threadPoolTaskScheduler.setPoolSize(20);
        threadPoolTaskScheduler.setThreadNamePrefix("taskExecutor-");
        threadPoolTaskScheduler.initialize();
    }

    private TaskExecutors() {
    }

    /**
     * 创建定时任务池（常驻线程）
     *
     * @return
     */
    public static ScheduledExecutorService getScheduledThreadPool() {
        return scheduledThreadPoolExecutor;
    }

    /**
     * 创建定时任务池（常驻线程）
     *
     * @return
     */
    public static ThreadPoolTaskScheduler getThreadPoolTaskScheduler() {
        return threadPoolTaskScheduler;
    }
}
